<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Playlist extends MY_Controller {
    
    function Playlist()
    {
        parent::__construct();
        $this->load->model('playlist_mod');
        $this->_checktoken();
    }

    public function create()
    {
        $result = array();
        $playlist_name = $this->input->post('playlist_name');
        $username = $this->session->userdata('username');
        if(!empty($playlist_name) && !empty($username)) {
            //check playlist name
            $checkname  = $this->playlist_mod->check_name($playlist_name,$username);
            if(!$checkname){ 
                $data_post = array (
                    'playlist_name' => $playlist_name,
                    'username' => $username,
                    'insert_date' => date('Y-m-d H:i:s')
                );
                $this->playlist_mod->add($data_post);
                $result['result'] = TRUE;
            } else {
                $result['result'] = FALSE;
                $result['msg'] = 'playlist name already exist';
            }
        } else {
            $result['result'] = FALSE;
            $result['msg'] = 'data blank';
        }
        $this->_outputjson($result);
    }
    
    public function add_song()
    {
        $result = array();
        $idsong = $this->input->post('idsong');
        $idplay = $this->input->post('idplaylist');
        if(!empty($idsong) && !empty($idplay)) {
            $checksong  = $this->playlist_mod->check_song($idplay, $idsong);
            if(!$checksong){
                $data_post = array (
                    'id_user_playlist' => $idplay,
                    'id_song' => $idsong
                );
                $this->playlist_mod->add_song($data_post);
                $result['result'] = TRUE;
            } else {
                $result['result'] = FALSE;
                $result['msg'] = 'song already exist';
            }
        } else {
            $result['result'] = FALSE;
            $result['msg'] = 'data blank';
        }
        $this->_outputjson($result);
    }
    
    public function delete_song()
    {
        $result = array();
        $idsong = $this->input->post('idsong');
        $idplay = $this->input->post('idplaylist');
        if(!empty($idsong) && !empty($idplay)) {
            $this->playlist_mod->delete_song($idsong, $idplay);
            $result['result'] = TRUE;
        } else {
            $result['result'] = FALSE;
            $result['msg'] = 'data blank';
        }
        $this->_outputjson($result);
    }
    
    public function browse()
    {
        $result = array();
        $username = $this->session->userdata('username');
        if(!empty($username)) {
            $playlists  = $this->playlist_mod->get_byuser($username);
            if($playlists) {
                $result['result'] = TRUE;
                $result['playlist'] = $playlists;
            } else {
                $result['result'] = FALSE;
            }
        } else {
            $result['result'] = FALSE;
        }
        $this->_outputjson($result);
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */